package db.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import db.DBManager;

/**
 * 通用函数处理类
 * 
 * @author 马远志
 * 
 */
public class DBCommon {
	private static DateFormat f1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 调用数据库服务器时间
	 * 
	 * @return
	 */
	public static Date getServerTime() {
		Date tempD = null;
		Connection conn = null;
		PreparedStatement stm = null;
		ResultSet rs = null;
		String sql = "{call getServerTime()}";
		try {
			conn = DBManager.getConnection();
			// 调用mysql数据库的存储过程
			stm = conn.prepareCall(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				tempD = rs.getTimestamp("serverTime");
				try {
					tempD = f1.parse(f1.format(tempD));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				stm.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return tempD;
	}
}
